package education.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import education.dao.TestQuestionsDao;
import education.exception.DAOException;
import education.model.Course;
import education.model.TestQuestions;

@Component("testQuestionsDao")
public class TestQuestionsDaoImpl implements TestQuestionsDao {
	private HibernateTemplate hibernateTemplate;

	public boolean delete(int id) {
		Session session = hibernateTemplate.getSessionFactory().openSession();
		Query query = session
				.createQuery("delete TestQuestions testQuestions where testQuestions.id=?");
		query.setInteger(0, id);
		int re = query.executeUpdate();
		session.close();
		if (re != 0) {
			return true;
		}
		return false;
	}

	public void delete(TestQuestions testQuestions) {
		hibernateTemplate.delete(testQuestions);
	}

	public int getCount() {
		Session session = hibernateTemplate.getSessionFactory().openSession();
		Query query = session.createQuery("select count(*) from TestQuestions");
		int count = Integer.parseInt(query.list().get(0).toString());
		session.close();
		return count;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public TestQuestions loadById(int id) {
		System.out.print("TestQuestionsDaoImpl is loadById");
		Session session = hibernateTemplate.getSessionFactory()
				.getCurrentSession();
		return (TestQuestions) session.get(TestQuestions.class, id);
	}

	public List<TestQuestions> paging(int pageSize, int pageCount, int rowCount) {
		List<TestQuestions> list;
		pageSize = (pageSize - 1) * rowCount;
		Session session = hibernateTemplate.getSessionFactory().openSession();
		Query query = session.createQuery("from TestQuestions").setFirstResult(
				pageSize).setMaxResults(rowCount);
		System.out.println(query);
		list = query.list();
		session.close();
		return list;
	}

	public void save(TestQuestions testQuestions) {
		hibernateTemplate.save(testQuestions);
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public boolean update(TestQuestions testQuestions) {
		hibernateTemplate.update(testQuestions);
		return true;
	}

	public List<Course> getCourseList() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	
}
